package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    // 新增菜品
    @Transactional
    @Override
    public void add(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.add(dish);

        List<DishFlavor> flavorList = dishDTO.getFlavors();
        Long id = dish.getId();
        insertFlavorList(flavorList, id);
    }

    // 批量新增口味
    private void insertFlavorList(List<DishFlavor> flavorList, Long id) {
        if(!CollectionUtils.isEmpty(flavorList)){
            for (DishFlavor dishFlavor : flavorList) {
                dishFlavor.setDishId(id);
            }
            dishFlavorMapper.addFlavor(flavorList);
        }
    }

    // 菜品分页查询
    @Override
    public PageResult pageQuery(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        List<DishVO> list = dishMapper.pageQuery(dto);
        Page<DishVO> p = (Page<DishVO>) list;
        return new PageResult(p.getTotal(),p.getResult());
    }

    // 批量删除菜品
    @Override
    @Transactional
    public Integer deleteByIds(List<Long> ids) {
        // 查询符合删除条件的菜品
        int count =  dishMapper.selectByIds(ids);
        //符合删除菜品条件的就删除
        if(count > 0){
            dishMapper.deleteByIds(ids);
            dishFlavorMapper.deleteByIds(ids);
            return 1;
        }
        return 0;
    }

    // 根据id查询菜品
    @Override
    @Transactional
    public DishVO selectById(Long dishId) {
        // 查询菜品
        DishVO dishVO = dishMapper.selectById(dishId);
        // 查询口味
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectByDishId(dishId);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    // 修改菜品
    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        // 修改菜品信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        // 修改口味: 1. 删除原有口味   2. 新增口味
        List<DishFlavor> flavorList = dishDTO.getFlavors();
        Long id = dish.getId();
        dishFlavorMapper.deleteByDishId(id);
        insertFlavorList(flavorList,id);

    }

    // 根据分类id查询菜品
    @Override
    public List<Dish> selectByCategoryId(Integer categoryId) {
        return dishMapper.selectByCategoryId(categoryId);
    }

}
